using System;
using System.Collections.Generic;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace Life
{
    // Main panel for Load screen
    public class LoadPanel : ScrollingPanelControl
    {
        Control resultListControl = null;

        SpriteFont detailFont;

        ScreenManager _screenManager;
        IGameManager _gameManager;

        List<string> _confirmDeleteButtons;
        const int _confirmDeleteOkButtonIndex = 0;

        public LoadPanel(ScreenManager screenManager, bool isColumn, float spacing)
            : base(isColumn, spacing)
        {
            _screenManager = screenManager;
            _gameManager = (IGameManager)Game1.GetService(typeof(IGameManager));

            ContentManager content = _screenManager.Game.Content;

            SpriteFont titleFont = content.Load<SpriteFont>("Font\\MenuTitle");
            SpriteFont headerFont = content.Load<SpriteFont>("Font\\MenuHeader");
            
            detailFont = content.Load<SpriteFont>("Font\\MenuDetail");

            _confirmDeleteButtons = new List<string>();
            _confirmDeleteButtons.Add("Cancel");
            _confirmDeleteButtons.Insert(_confirmDeleteOkButtonIndex, "Ok");

            AddChild(new TextControl("Load Game", titleFont));
            AddChild(new TextControl("Select Existing", headerFont, Color.DarkGreen));

            LoadSavedGames();
        }

        private void LoadSavedGames()
        {
            PanelControl newList = new PanelControl(true, 10);

            string fileExtension = Constants.SaveFileExtension;

            string[] fileNameList = FileManager.GetFileNames("*" + fileExtension);

            foreach (string fileName in fileNameList)
            {
                // File extension is not displayed to the user
                string displayName = fileName.Remove(fileName.Length - fileExtension.Length);

                TextControl child = new TextControl(displayName, detailFont, Color.White, new Vector2(0, 0));
                child.Held += DeleteSelected;
                child.Tapped += ExistingFileSelected;

                newList.AddChild(child);
            }

            if (resultListControl != null)
            {
                RemoveChild(resultListControl);
            }
            resultListControl = newList;
            AddChild(resultListControl);
            Layout(0, 0);
        }

        private void DeleteSelected(object sender, EventArgs args)
        {
            if (sender is TextControl)
            {
                TextControl textControl = sender as TextControl;

                Guide.BeginShowMessageBox("Confirm Delete", "Delete saved game \"" + textControl.Text + "\" ?", 
                    _confirmDeleteButtons, _confirmDeleteOkButtonIndex, MessageBoxIcon.Alert, ConfirmDelete, textControl.Text);
            }
        }

        protected void ConfirmDelete(IAsyncResult r)
        {
            int? b = Guide.EndShowMessageBox(r);

            if (b.HasValue && b.Value == _confirmDeleteOkButtonIndex)
            {
                string fileName = (string)r.AsyncState;
                FileManager.Delete(fileName + Constants.SaveFileExtension);

                LoadSavedGames();
            }
        }

        private void ExistingFileSelected(object sender, EventArgs args)
        {
            if (sender is TextControl)
            {
                TextControl textControl = sender as TextControl;

                SaveGameData savedGame = FileManager.Load<SaveGameData>(textControl.Text + Constants.SaveFileExtension);
                _gameManager.Update(savedGame);

                LoadingScreen.Load(_screenManager, false, null, new GameplayScreen());
            }
        }
    }
}
